package com.bw.module_home.view

import androidx.core.widget.NestedScrollView
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter


import com.bw.module_home.model.HomeCateData
import com.bawei.common.entity.BannerEntity
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmFragment
import com.bw.module_home.R
import com.bw.module_home.adapter.HomeBannerAdapter
import com.bw.module_home.adapter.HomeCateAdapter
import com.bw.module_home.adapter.HomeNoticeAdapter
import com.bw.module_home.adapter.HotServiceAdapter
import com.bw.module_home.databinding.FragmentHomeBinding
import com.bw.module_home.viewmodel.HomeViewModel
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer

@Route(path = RouterPath.PATH_FRAGMENT_HOME)
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding>() {

    private val homeCateAdapter = HomeCateAdapter()
    private lateinit var hotServiceAdapter:HotServiceAdapter
    private val homeListAdapter: HomeNoticeAdapter = HomeNoticeAdapter()
    private val searchHotWords = arrayListOf("热门视频", "今日头条视频", "抖音视频", "快手视频")
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter: HomeBannerAdapter
    private val vm : HomeViewModel by activityViewModels()
    private var page:Int=0
    private var size:Int=20

    override fun initViews() {
        super.initViews()

        //设置textBanner的data---搜索热词
        mDataBinding.tvbSearch.run {
            setDatas(searchHotWords)
            setItemOnClickListener { data, position ->
                //点击搜索热词后跳转
//                ARouter.getInstance().build("")
//                    .withString("hotWords", data)
//                    .navigation()
            }
        }

        //设置banner2
        bannerData.run {
            add(BannerEntity(0, com.bw.lib_resource.R.drawable.wellcome_0, ""))
            add(BannerEntity(1, com.bw.lib_resource.R.drawable.wellcome_1, ""))
            add(BannerEntity(2, com.bw.lib_resource.R.drawable.wellcome_2, ""))
        }

        mBannerAdapter = HomeBannerAdapter(bannerData)
        mDataBinding.banner.run {
            setAdapter(mBannerAdapter)
            //关联Banner与Activity的生命周期
            addBannerLifecycleObserver(this@HomeFragment.viewLifecycleOwner)
            //设置指示器的样式
            indicator = CircleIndicator(this@HomeFragment.requireContext())
            setPageTransformer(AlphaPageTransformer())
        }

        //一级分类列表
        homeCateAdapter.data.addAll(HomeCateData.homeCateList)
        homeCateAdapter.notifyDataSetChanged()
        mDataBinding.rv1.adapter = homeCateAdapter
        mDataBinding.rv1.layoutManager = GridLayoutManager(activity, 4)

        hotServiceAdapter= HotServiceAdapter(HomeCateData.homeCateList)

        //一级分类点击事件
        homeCateAdapter.setOnItemClickListener { adapter, view, position ->
            when(homeCateAdapter.getItem(position).title){
                "车位管理"->{
                    ARouter.getInstance().build(RouterPath.PATH_PARKING_ACTIVITY).navigation()
                }
                "访客登记"->{
                    ARouter.getInstance().build(RouterPath.PATH_MAINTENANCE_ACTIVITY).navigation()
                }
                "物业报修"->{
                    ARouter.getInstance().build(RouterPath.PATH_REPAIR_ACTIVITY).navigation()
                }
                "考勤打卡"->{
                    ARouter.getInstance().build(RouterPath.PATH_CLOCK_IN_ACTIVITY).navigation()
                }
                "房源信息"->{
                    ARouter.getInstance().build(RouterPath.PATH_HOUSE_ACTIVITY).navigation()

                }
                "商家推荐"->{
                    ARouter.getInstance().build(RouterPath.PATH_STORE_ACTIVITY).navigation()
                }
                "月卡会员"->{
                    ARouter.getInstance().build(RouterPath.PATH_MONTH_VIP_ACTIVITY).navigation()
                }
                "设备管理"->{
                    ARouter.getInstance().build(RouterPath.PATH_DEVICE_ACTIVITY).navigation()
                }

            }
        }

        //公告列表
        mDataBinding.rv2.adapter = homeListAdapter
        mDataBinding.rv2.layoutManager = LinearLayoutManager(context)


        mDataBinding.nsvContainer.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
            //滚动到底部时触发事件
            val isAtBottom = !v.canScrollVertically(1) // 参数1表示向下滑动
            if (isAtBottom) {
                vm.getHomeList(page++, 20)
            }

            val isAtTop = !v.canScrollVertically(-1)  // 参数-1表示向上滑动
            if (isAtTop) {
                page = 0
                homeListAdapter.data.clear()
                vm.getHomeList(page,20)
            }
        })
    }

    override fun loadData() {
        vm.getHomeList(page, size)
    }

    override fun observeDataChange() {
        vm.mSuccessLiveData.observe(this) {
            homeListAdapter.data.addAll(it.reversed().filter { item->item.title.length==4 })
            homeListAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_home

}